package com.tg.service.eventbus2.springconfig;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class LoadConfig {

    public static String read(String fileName) {
        try {
            String path = LoadConfig.class.getClassLoader().getResource(fileName).getPath();
            File file = new File(path);

            List<String> readLines = Files.readLines(file, Charsets.UTF_8);
            List<String> res = readLines.stream().filter(s -> !s.trim().startsWith("--")).collect(Collectors.toList());
            String join = Joiner.on(" ").join(res);
            return join;
        } catch (IOException ex) {
            return "";
        }
    }

    public static List<String> readAsRow(String fileName) {
        try {
            String path = LoadConfig.class.getClassLoader().getResource(fileName).getPath();
            File file = new File(path);
            return Files.readLines(file, Charsets.UTF_8);
        } catch (IOException ex) {
            return Lists.newArrayList();
        }
    }
}
